package com.tbit.uqbike.client.util;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import java.io.*;
import java.util.*;

/**
 * 文件工具类
 *
 * @author Leon 2014-5-16 上午09:32:09
 */
@Slf4j
public class FileUtils {

    /**
     * 删除单个文件
     *
     * @param sPath 被删除文件的文件名
     * @return 单个文件删除成功返回true，否则返回false
     */
    public static boolean deleteFile(String sPath) {
        boolean flag = false;
        File file = new File(sPath);
        // 路径为文件且不为空则进行删除
        if (file.isFile() && file.exists()) {
            file.delete();
            flag = true;
        }
        return flag;
    }

    /**
     * 创建目录
     *
     * @param destDirName 目标目录名
     * @return 目录创建成功返回true，否则返回false
     */
    public static boolean createDir(String destDirName) {
        File dir = new File(destDirName);
        if (dir.exists()) {
            return false;
        }
        if (!destDirName.endsWith(File.separator)) {
            destDirName = destDirName + File.separator;
        }
        // 创建单个目录
        if (dir.mkdirs()) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 转换一个xml格式的字符串到json格式
     *
     * @param file java.io.File实例是一个有效的xml文件
     * @return 成功反回json 格式的字符串;失败反回null
     */
    @SuppressWarnings("unchecked")
    public static String xml2JSON(File file) {
        JSONObject obj = new JSONObject();
        try {
            SAXBuilder sb = new SAXBuilder();
            Document doc = sb.build(file);
            Element root = doc.getRootElement();
            obj.put(root.getName(), iterateElement(root));
            return obj.toString();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }


    /**
     * 一个迭代方法
     *
     * @param element : org.jdom.Element
     * @return java.util.Map 实例
     */
    @SuppressWarnings("unchecked")
    private static Map iterateElement(Element element) {
        List jiedian = element.getChildren();
        Element et = null;
        Map obj = new HashMap();
        List list = null;
        for (int i = 0; i < jiedian.size(); i++) {
            list = new LinkedList();
            et = (Element) jiedian.get(i);
            if (et.getTextTrim().equals("")) {
                if (et.getChildren().size() == 0) {
                    continue;
                }
                if (obj.containsKey(et.getName())) {
                    list = (List) obj.get(et.getName());
                }
                list.add(iterateElement(et));
                obj.put(et.getName(), list);
            } else {
                if (obj.containsKey(et.getName())) {
                    list = (List) obj.get(et.getName());
                }
                list.add(et.getTextTrim());
                obj.put(et.getName(), list);
            }
        }
        return obj;
    }


    /**
     * 以行为单位读取文件，常用于读面向行的格式化文件
     */
    public static List<String> readFileByLines(String fileName) {
        List<String> list = new ArrayList<>();
        File file = new File(fileName);
        BufferedReader reader;
        reader = null;
        try {
            System.out.println("以行为单位读取文件内容，一次读一整行：");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
                list.add(tempString);
                //System.out.println("line " + line + ": " + tempString);
                line++;
            }
            reader.close();
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        return list;
    }

    /**
     * 读取集合，存入文件
     *
     * @param longs
     * @param fileName
     * @throws FileNotFoundException
     */
    public static void witeList(List<Object> longs, String fileName) throws FileNotFoundException {
        for (int i = 0; i < longs.size(); i++) {
            File file = new File(fileName);
            FileOutputStream fos1 = new FileOutputStream(file, true);
            // 创建FileOutputStream对应的PrintStream，方便操作。PrintStream的写入接口更便利
            PrintStream out1 = new PrintStream(fos1);
            String str = longs.get(i) + "\r\n";
            out1.print(str);
            out1.close();
        }
    }


    public static void main(String[] args) throws FileNotFoundException {

        List<String> list = readFileByLines("e:\\new11.txt");

        List<String> arrays = new ArrayList<>();

        List<String> max = new ArrayList<>();

        List<String> min0 = new ArrayList<>();
        List<String> min5 = new ArrayList<>();

        for (String tmp : list) {
            String[] strings = tmp.split(",");
            for (String t : strings) {
                if (t.contains("耗时")) {
                    String m = t.substring(2);
                    arrays.add(m);
                    if (Integer.valueOf(m) > 10000) {
                        max.add(m);
                    } else if (Integer.valueOf(m) > 5000 && Integer.valueOf(m) <= 10000) {
                        min5.add(m);
                    } else if (Integer.valueOf(m) <= 5000) {
                        min0.add(m);
                    }
                }
            }
        }
        //System.out.println(xml2JSON(new File("G:\\xml\\text.xml")));
        System.out.println("0-5秒" + ((double) min0.size() / arrays.size() + "5-10秒" + ((double) min5.size() / arrays.size())) + "大于10秒" + ((double) max.size() / arrays.size()));
    }

}
